package com.spider.Test;
//数字华容道
public class Demo2 {
    public static void main(String[] args) {
    start(6);
    }
    public static void start(int n)
    {
    //1.创建二维数组
    int[][] arr = new int[n][n];
    //2.遍历二维数组并赋值
        int count = 1;
        for (int i = 0; i < arr.length; i++) {
            for (int j = 0; j < arr[i].length; j++) {
                arr[i][j] =  count++;
            }
        }
        printarr(arr);
    //3.打乱二维数组中的元素顺序
    for (int i = 0; i < arr.length; i++) {
        for (int j = 0; j < arr[i].length; j++) {
            int x = (int)(Math.random()*arr.length);
            int y = (int)(Math.random()*arr.length);
            //交换
            int temp = arr[i][j];
            arr[i][j] = arr[x][y];
            arr[x][y] = temp;
        }
    }
        System.out.println("打乱后:");
    printarr(arr);
    }
    public static void printarr(int[][] arr)
    {
        for (int i = 0; i < arr.length; i++) {
            for (int j = 0; j < arr[i].length; j++) {
                System.out.print(arr[i][j]+"\t");
            }
            System.out.println();
        }
    }
}
